//Cracking the coding interview (5th), 1_6
//Write an algorithm such that if an element in an MxN matrix is 0,
//its entire row and column are set to 0.

#include<stdio.h>
#define M 3
#define N 5

void SetZero(int p[][N])
{
	int row[M]={0};
	int col[N]={0};
	for(int i=0;i<M;i++)
	{
		for(int j=0;j<N;j++)
		{
			if(p[i][j]==0)
			{
				row[i]=1;
				col[j]=1;
			}
		}
	}
	for(int r=0;r<M;r++)
	{
		if(row[r])
		{
			for(int c=0;c<N;c++)
				p[r][c]=0;
		}
	}
	for(int c=0;c<N;c++)
	{
		if(col[c])
		{
			for(int r=0;r<M;r++)
				p[r][c]=0;
		}
	}
}

int main()
{
	int a[M][N]={{1,2,0,3,4},{0,1,2,3,4},{1,2,3,4,5}};
	SetZero(a);
	for(int i=0;i<M;i++)
	{
		for(int j=0;j<N;j++)
		{
			printf("%d ",a[i][j]);
			if(j==N-1)
				printf("\n");
		}
	}
	return 0;
}
